package com.study.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import androidx.core.view.ViewPropertyAnimatorListener;
import androidx.interpolator.view.animation.FastOutLinearInInterpolator;
import androidx.interpolator.view.animation.LinearOutSlowInInterpolator;

/**
 * author : xujianhong
 * date   : 2022/11/9
 * desc   : 自定义Behavior 引用的时候必须是 包名 + 类名
 * version: 1.0
 */
public class ScaleBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {

    //加速匀速插值器
    private FastOutLinearInInterpolator mFastOutLinearInInterpolator = new FastOutLinearInInterpolator();
    //匀速减速插值器
    private LinearOutSlowInInterpolator mLinearOutSlowInInterpolator = new LinearOutSlowInInterpolator();
    //当前动画执行的状态
    private boolean isRunning;

    public ScaleBehavior(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    //滑动方向
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }

    //开始滑动
    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

        //判断条件 Y轴滑动的距离大于零 是向下滑动、当前的控件必须是显示状态、动画必须执行完毕
        if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE && !isRunning) {
            //向下滑动 缩放隐藏
            scaleHide(child);
        } else if (dyConsumed < 0 && !isRunning && child.getVisibility() == View.INVISIBLE) {//Y轴滑动的距离小于0为向上滑动、动画执行完毕、控件为隐藏状态
            //向上滑动 缩放显示
            scaleShow(child);
        }
    }


    private void scaleShow(V child){
        child.setVisibility(View.VISIBLE);
        ViewCompat.animate(child)
                .scaleX(1)
                .scaleY(1)
                .setDuration(500)
                .setInterpolator(mLinearOutSlowInInterpolator)
                .setListener(new ViewPropertyAnimatorListener() {
                    @Override
                    public void onAnimationStart(View view) {
                        isRunning = true;
                    }

                    @Override
                    public void onAnimationEnd(View view) {
                        isRunning = false;
                    }

                    @Override
                    public void onAnimationCancel(View view) {
                        isRunning = false;
                    }
                });

    }

    private void scaleHide(V child){
        ViewCompat.animate(child)
                .scaleX(0)
                .scaleY(0)
                .setDuration(500)
                //设置差值器
                .setInterpolator(mFastOutLinearInInterpolator)
                .setListener(new ViewPropertyAnimatorListener() {
                    @Override
                    public void onAnimationStart(View view) {
                        isRunning = true;
                    }

                    @Override
                    public void onAnimationEnd(View view) {
                        //动画执行完成后 将控件设置成不可见状态
                        child.setVisibility(View.INVISIBLE);
                        isRunning = false;

                    }

                    @Override
                    public void onAnimationCancel(View view) {
                        isRunning = false;
                    }
                });

    }
}
